#!/usr/bin/python
import os, sys, string, commands

class XXDos2Unix:
	def __init__(self):
		pass
	
	def printHelp(self, cmd):
		print 'Please use the following cmd:'
		print '    ' + cmd + ' dir'
		print '    ' + cmd + ' file'
		print 'e.g.'
		print '    ' + cmd + ' /home/fkong/tmp'
		print '    ' + cmd + ' /home/fkong/build.xml' 
	
	def dos2UnixForDir(self, dir):
		list = self.getList(dir)
		for i in range(0, len(list)):
			self.dos2UnixForFile(str(list[i]))

	def dos2UnixForFile(self, file):
		commands.getstatusoutput("dos2unix " + file)
		
	def getList(self, dir):
		list = []
		files = os.listdir(dir)
		for file in files:
			file = os.path.join(dir, file)
			if os.path.isdir(file):
				list += self.getList(file)
			else :
				list.append(file)
		return list
	
if len(sys.argv) != 2:
	t = XXDos2Unix()
	t.printHelp(sys.argv[0])
else:
	t = XXDos2Unix()
	if os.path.isdir(sys.argv[1]):
		t.dos2UnixForDir(sys.argv[1])
	else:
		t.dos2UnixForFile(sys.argv[1])
